﻿#include <Windows.h>
#include "CGame.h"

int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPWSTR lpCmdnLine,int nCmdShow) {
	MSG msg;
	int done = 0;
	CGame game(hInstance,SCREEN_WIDTH,SCREEN_HEIGHT,APP_TITLE);
	if(!game.InitWindow()) {
		MessageBox(NULL,"Khong the khoi tao game","Loi",MB_OK);
		return 0;
	}
	// khởi tạo dữ liệu
	game.InitData();
	// các biến này dùng để tính toán game time
	LARGE_INTEGER startTime;
	LARGE_INTEGER frequence;
	float cntPerSecond = 0;
	float gameTime = 0;
	LARGE_INTEGER endTime;
	float delay = 0;
	ZeroMemory(&msg,sizeof(msg));
	QueryPerformanceFrequency(&frequence);
	cntPerSecond = 100.0f / (float)frequence.QuadPart;
	QueryPerformanceCounter(&endTime);
	while(!done) {
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
			if(msg.message == WM_QUIT) {
				done = 1;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		} else {
			QueryPerformanceCounter(&startTime);
			//delay += gameTime;
			//if(delay > gameTime * 3) {
			game.Update(gameTime);
			game.Render();
			//delay = 0;
			//}
			QueryPerformanceCounter(&endTime);
			gameTime = ((float)endTime.QuadPart - (float)startTime.QuadPart) * cntPerSecond;
		}
	}
	game.Release();
	return (int)msg.wParam;
}